// pages/listings/listings.js
var count_down = require('../../utils/countDown.js');
var wxScan = require('../../utils/wxScan.js');
Page({
  data: {
    host: getApp().globalData.host,
    hidden: false,
    listings: [],
    scrollTop: 0,
    scrollHeight: 0,
    page: {
      pageNo: 1,
      pageSize: 8
    },
    hasMore: true,
    countDown: { },
    orderby:'',
    sortasc:'asc',
    searchName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.getData();
  },

  getData: function () {
    var that = this;
    var orderPrice = "";
    if (that.data.orderby == 'price') {
      orderPrice = that.data.sortasc;
    }
    wx.request({
      url: that.data.host + '/webapp/listing/all.html',
      header: getApp().globalData.header,
      data: {orderPrice: orderPrice, listingName: that.data.searchName, pageSize: that.data.page.pageSize, pageNo: that.data.page.pageNo },
      method: 'POST',
      success: function (res) {
        if (res.data.results.length > 0) {
          var resData = res.data.results;
          var listings = that.data.listings;
          for (var i = 0; i < resData.length; i++) {
            count_down.countDown('', resData[i].expireTime, that, resData[i].listingCode);
            listings.push(resData[i]);
          }
          that.setData({
            listings: that.data.listings,
            hidden: true
          })
          that.data.page.pageNo++;
        } else {
          that.setData({
            hasMore: false,
            hidden: true
          });
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },

  getDetail: function (event) {
    var listingId = event.currentTarget.dataset.listingId;
    wx.navigateTo({
      url: '../listing/listing?listingId=' + listingId,
    })
  },
  //页面滑动到底部
  bindDownLoad: function () {
    var that = this;

    if (that.data.hasMore) {
      that.getData();
      //查询期间设置为不得再次查询
      that.setData({
        hasMore: false
      });
    }
  },
  queryList:function(event) {
    var clickTab = event.currentTarget.dataset.orderby;
    var datasort = 'asc';
    if (clickTab == this.data.orderby) {
      if ('price' == clickTab && datasort == this.data.sortasc) {
        datasort = 'desc';
      } else if ('' == clickTab)
        return;
    }
    //初始化当前页
    this.setData({
      hidden: false,
      listings: [],
      page: {
        pageNo: 1,
        pageSize: 8
      },
      hasMore: true,
      orderby: clickTab,
      sortasc: datasort
    });
    this.getData();
  },
  //搜索
  inputSearch: function (e) {
    this.setData({
      searchName: e.detail.value
    })
  },
  searchData:function() {
    this.setData({
      hidden: false,
      listings: [],
      page: {
        pageNo: 1,
        pageSize: 8
      },
      hasMore: true
    });
    this.getData();
  },
  scanCode: function () {
    var that = this;
    wx.scanCode({
      success: function (res) {
        if ("scanCode:ok" == res.errMsg) {//扫描成功
          wxScan.wxScan(res.result);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})